# 文件名: seleniums
# 当前用户: shiping.zheng
# 当前日期: 2024/9/13
# 当前时间: 10:17
# 项目名称: Flag
"""
方式：使用ocr光学识别获取验证码
    1.登录页面 2，定位验证码图片位置 3.获取图像的base64数据 4.base64数据转为图片 5.图片优化 6.ocr识别图片
    1.登录页面 2，定位验证码图片位置 3.获取图像的base64数据 4.base64数据转为图片 4.ocr识别图片
    1.登录页面 2，定位验证码图片位置 3.截取保存验证码图片 4.ocr识别图片验证码
"""
import requests
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import io
import base64
from PIL import Image
import pytesseract
import cv2
import numpy as np
import redis
from rediscluster import RedisCluster


# 设置EdgeDriver路径
edge_driver_path = 'C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe'
# 创建Edge选项
edge_options = Options()
# 你可以在这里添加更多的选项，例如无头模式
# edge_options.add_argument("--headless")
# 创建Edge浏览器实例
service = Service(edge_driver_path)
driver = webdriver.Edge(service=service, options=edge_options)

driver.get('https://xuntian-test2.tclpv.com/getechLogin')
time.sleep(2)
# 使用 XPath 定位输入框
input_element = driver.find_element(By.XPATH,"//input[@placeholder='请输入用户名']")

# 在输入框中输入文本 "admin"
input_element.send_keys("admin")
time.sleep(1)
input_element = driver.find_element(By.XPATH,"//input[@type='password']")
input_element.send_keys("tcl123")
time.sleep(1)


# 使用 XPath 定位验证码图像
captcha_element = driver.find_element(By.XPATH, "//img[@class='code-img']")

# 获取图像的 base64 数据
image_base64 = captcha_element.get_attribute("src").split(",")[1]
# print(image_base64)

# 将 base64 数据转换为图像
image_data = base64.b64decode(image_base64)
image = Image.open(io.BytesIO(image_data))

# 保存图像到本地文件
image_path = r'C:\Users\shiping.zheng\Desktop\image\captcha.png'
image.save(image_path)
time.sleep(1)
captcha_image = Image.open(image_path)
captcha_text = pytesseract.image_to_string(captcha_image)
# # 使用 OpenCV 读取图像并进行预处理
# image_cv = cv2.imread(image_path)
#
# if image_cv is None:
#     print("无法读取图像文件:", image_path)
# else:
#     # 灰度化
#     gray = cv2.cvtColor(image_cv, cv2.COLOR_BGR2GRAY)
#
#     # 自适应阈值二值化
#     binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
#                                    cv2.THRESH_BINARY_INV, 11, 2)
#
#     # 去噪（可选）
#     kernel = np.ones((1, 1), np.uint8)
#     binary = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
#
#     # 保存预处理后的图像（可选）
#     preprocessed_image_path = r'C:\Users\shiping.zheng\Desktop\image\preprocessed_captcha.png'
#     cv2.imwrite(preprocessed_image_path, binary)
#
#     # 使用 Tesseract OCR 识别图像中的文本
#     captcha_text = pytesseract.image_to_string(binary, config='--psm 6')
#     print("识别的验证码内容:", captcha_text)
#
#     # 打开图片
#     image1 = Image.open(preprocessed_image_path)
#     image2 = Image.open(image_path)
#     # OCR 识别
#     text = pytesseract.image_to_string(image1)
#     text2 = pytesseract.image_to_string(image2)
#     print(text)
#     print(text2)




# # 截取验证码图片
# captcha_element.screenshot('captcha.png')
#
# # 打开图片并使用 OCR 识别
# captcha_image = Image.open('captcha.png')
# captcha_text = pytesseract.image_to_string(captcha_image)
#
# print(f"识别的验证码是: {captcha_text}")
# time.sleep(1)

input_element = driver.find_element(By.XPATH,"//input[@placeholder='请输入左图结果']")
input_element.send_keys(captcha_text)
login_button = driver.find_element(By.XPATH, "//button[span[text()='登录']]")
login_button.click()
input("Press Enter to close the browser...")